package com.school.sports.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.school.sports.entity.SystemConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 系统配置Mapper接口
 */
@Mapper
public interface SystemConfigMapper extends BaseMapper<SystemConfig> {

    /**
     * 根据配置键查询配置值
     * @param configKey 配置键
     * @return 配置对象
     */
    @Select("SELECT * FROM system_config WHERE config_key = #{configKey} AND status = 1 AND deleted = 0")
    SystemConfig selectByConfigKey(@Param("configKey") String configKey);

    /**
     * 根据配置类型查询配置列表
     * @param configType 配置类型
     * @return 配置列表
     */
    @Select("SELECT * FROM system_config WHERE config_type = #{configType} AND status = 1 AND deleted = 0 ORDER BY created_at ASC")
    List<SystemConfig> selectByConfigType(@Param("configType") String configType);

    /**
     * 检查配置键是否存在
     * @param configKey 配置键
     * @param excludeId 排除的ID（用于更新时检查）
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM system_config WHERE config_key = #{configKey} AND deleted = 0 " +
            "AND (#{excludeId} IS NULL OR id != #{excludeId})")
    int countByConfigKey(@Param("configKey") String configKey, @Param("excludeId") Long excludeId);

    /**
     * 查询所有启用的系统配置（用于缓存）
     * @return 配置列表
     */
    @Select("SELECT * FROM system_config WHERE status = 1 AND deleted = 0 ORDER BY config_type, config_key")
    List<SystemConfig> selectAllEnabledConfigs();
}